using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class TouchEventHandler : MonoBehaviour ,IPointerDownHandler,IPointerUpHandler,IDragHandler
{
    public UnityAction ClickEvent;
    public UnityAction<Vector3, Vector3> DragStartEvent;
    public UnityAction<Vector3, Vector3> DragEvent;
    public UnityAction<Vector3, Vector3> DragEndEvent;

    private bool onDrag;
    private Vector3 lastMousePos;

    private void OnMouseDown()
    {
        lastMousePos = Input.mousePosition;
    }

    private void OnMouseDrag()
    {
        if (!onDrag && Input.mousePosition != lastMousePos)
        {
            onDrag = true;
            Debug.Log("开始拖拽");
            DragStartEvent?.Invoke(lastMousePos, Input.mousePosition);
            return;
        }

        DragEvent?.Invoke(lastMousePos, Input.mousePosition);

        lastMousePos = Input.mousePosition;
    }

    private void OnMouseUp()
    {
        if (onDrag)
        {
            onDrag = false;
            Debug.Log("停止拖拽");
            DragEndEvent?.Invoke(lastMousePos, Input.mousePosition);
        }
        else if (lastMousePos == Input.mousePosition)
        {
            Debug.Log("点击碰撞体");
            ClickEvent?.Invoke();
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        OnMouseDown();
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        OnMouseUp();
    }

    public void OnDrag(PointerEventData eventData)
    {
        OnMouseDrag();
    }
}